package com.work;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class BinaryOperation {
    private Integer one;
    private Integer two;
    private String aChar;
    private Integer result;
    private String BinaryOperationStr;
    private boolean flag;
    private static ArrayList<String> add = new ArrayList<>();
    private static ArrayList<String> low = new ArrayList<>();
    private Random random;
    private int numer = 50;
    private int count = 0;

    public BinaryOperation(){

    }
    
    public void start(Scanner scan){
        while(true){
            String flog = scan.next();
            switch(flog){
                case "1":
                    mix();
                    break;
                case "2":
                    addition();
                    break;
                case "3":
                    subtraction();
                    break;
                case "4":
                case "test":
                    test(scan);
                    break;
                case "0":
                case "exit":
                    System.out.println("~~~~~~正在退出~~~~~~");
                    break;
        }
        if (flog.equals("0") || flog.equals("exit")) {
            break;
        }
            System.out.println();
            System.out.println("-----退出'0/exit'或继续选择其他模式'1'混合加减，'2'加法 '3'减法 '4/test'手动输入测试-------");
        }
    }
    
    /**
     * BinaryOperation的构造函数，传入random
     * @param random
     */
    public BinaryOperation(Random random){
        this.random = random;
    }

    /**
     *  格式化式子
     * @return
     */
    public BinaryOperation createBinaryOperation() {
        count();
        /* 在生成式子时格式化字符，确保输出时对其*/
        BinaryOperationStr = String.format("%-2d %-3s %-2d = %-2d \t\t",one, aChar, two, result);
        return this;
    }

    /**
     * 该方法用于判断加减法，进行分类放入不同的算数集合中
     */
    public void count() {
        if (aChar == " - ") {
            result = this.one - this.two;
            flag = false;
        } else {
            result = this.one + this.two;
            flag = true;
        }
        if (result >= 100) {
            this.two -= 10;
            count();
        }
        if (result <= 0) {
            this.two -= 2;
            count();
        }
    }
    //混合运算
    public void mix(){
        int count = 0;
        int i = 0;
        while(i < numer){
            one = random.nextInt(1,100);
            two = random.nextInt(1,100);
            if (random.nextInt() % 2 == 1) {
                this.aChar = " - ";
            } else {
                this.aChar = " + ";
            }
            BinaryOperation BinaryOperation = this.createBinaryOperation();
            if (add.contains(BinaryOperation.getBinaryOperationStr())||low.contains(BinaryOperation.getBinaryOperationStr())){
                continue;
            }
            if (flag) {
                add.add(BinaryOperation.getBinaryOperationStr());
            }else {
                low.add(BinaryOperation.getBinaryOperationStr());
            }
            i++;
        }
        soutAddtion();
        System.out.println();
        soutSubtion();
    }
    //加法运算
    public void addition(){
        int i = 0;
        this.aChar = " + ";
        while(i < numer){
            one = random.nextInt(1,100);
            two = random.nextInt(1,100);
            BinaryOperation BinaryOperation = this.createBinaryOperation();
            if (add.contains(BinaryOperation.getBinaryOperationStr())){
                continue;
            }
            add.add(BinaryOperation.getBinaryOperationStr());
            i++;
        }
        soutAddtion();
    }
    //减法运算
    public void subtraction(){
        int i = 0;
        this.aChar = " - ";
        while(i < 50){
            one = random.nextInt(1,100);
            two = random.nextInt(1,100);
            BinaryOperation BinaryOperation = this.createBinaryOperation();
            if (low.contains(BinaryOperation.getBinaryOperationStr())){
                continue;
            }
            low.add(BinaryOperation.getBinaryOperationStr());
            i++;
        }
        soutSubtion();
        low.clear();
    }

    /**
     *  循环输入式子，四次输入，分别为加/减数，加减号，被加/减数，结果
     *  若式子错误不添加
     *  并记录
     */
    public void test(Scanner scan) {
        // 输入你想测试的模式
        int model = 0 ;
        while(true){
            System.out.println("----------输入你想测试的模式'1'混合加减，'2'加法 '3'减法 '4'手动输入测试 ---------");
            model= scan.nextInt();
            switch (model){
                case 1:
                    System.out.print("生成式子的次数：");
                    numer = scan.nextInt();
                    mix();
                    numer = 50;
                    break;
                case 2:
                    System.out.print("生成式子的次数：");
                    numer = scan.nextInt();
                    addition();
                    numer = 50;
                    break;
                case 3:
                    System.out.print("生成式子的次数：");
                    numer = scan.nextInt();
                    subtraction();
                    numer = 50;
                    break;
                case 4:
                    hand(scan);
                    break;
                case 0:
                    return;
            }
            System.out.println();
        }



    }

    /**
     * 手动输入式子
     * @param scan
     */
    public void hand(Scanner scan){
        while (true){
            try{
                System.out.print("(输入000测试输入退出)输入第一个数： ");
                String str = scan.next();
                if (str.equals("000"))
                    break;
                one =Integer.valueOf(str);
                System.out.print("输入符号： ");
                String input = scan.next();
                if (input.contains("-")){
                    aChar = " - ";
                }else if (input.contains("+")){
                    aChar = " + ";
                }else {
                    throw new Exception("输入符号应为+/-");
                }
                System.out.print("输入第二个数： ");
                two = scan.nextInt();
                System.out.print("输入结果： ");
                int resultT = scan.nextInt();
                if (aChar == " - ") {
                    result = this.one - this.two;
                    flag = false;
                } else {
                    result = this.one + this.two;
                    flag = true;
                }
                if (result == resultT && result >= 0 && result <= 100){
                    BinaryOperationStr = one + aChar + two + " = " + result + "\t\t";
                }else {
                    throw new Exception("结果有误");
                }
                if (add.contains(this.getBinaryOperationStr())||low.contains(this.getBinaryOperationStr())){
                    continue;
                }
                if (flag) {
                    add.add(this.getBinaryOperationStr());
                }else {
                    low.add(this.getBinaryOperationStr());
                }
            }catch(Exception e){
                System.out.println(e.getMessage());
                System.out.println("----------输入有误请重新输入-------------");
            }
        }
        int count = 0;
        if (!add.isEmpty()){
            soutAddtion();
        }
        if (!low.isEmpty()){
            soutSubtion();
        }
    }

    /**
     * 加法输出
     */
    public void soutAddtion(){
        System.out.println("-----------------下面是加法计算------------------");
        for (String str :add){
            if (count == 6){
                System.out.println();
                count = 0;
            }
            System.out.print(str);
            count++;
        }
        System.out.println();
        count = 0;
        add.clear();
    }

    /**
     * 减法输出
     */
    public void soutSubtion(){
        System.out.println("-----------------下面是减法计算------------------");
        for (String str :low){
            if (count == 6){
                System.out.println();
                count = 0;
            }
            System.out.print(str);
            count++;
        }
        count = 0 ;
        low.clear();
    }

    /**
     * 用于返回生成的式子
     * @return
     */
    public String getBinaryOperationStr() {
        return BinaryOperationStr;
    }

}